package com.leantech.android.browser.ui.shortcut

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.lifecycleScope
import androidx.lifecycle.repeatOnLifecycle
import androidx.recyclerview.widget.LinearLayoutManager
import com.cscj.android.repository.db.entity.ShortCutEntity
import com.cscj.android.repository.network.Result
import com.leantech.android.browser.databinding.FragmentRecyclerViewBinding
import com.leantech.android.browser.ui.shortcut.adapter.LinkItemAdapter
import com.leantech.android.browser.ui.shortcut.adapter.LinkShortCutAdapter
import com.leantech.android.browser.ui.shortcut.viewmodel.ShortCutViewModel
import com.leantech.android.browser.utils.failToast
import com.leantech.android.browser.utils.successToast
import com.cscj.android.utils.LOG
import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.launch
import org.koin.androidx.viewmodel.ext.android.activityViewModel

class LinkShortCutFragment : Fragment(), LinkItemAdapter.LinkItemCallback {

    private lateinit var binding: FragmentRecyclerViewBinding

    private lateinit var adapter: LinkShortCutAdapter

    private val viewModel by activityViewModel<ShortCutViewModel>()

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?
    ): View? {
        binding = FragmentRecyclerViewBinding.inflate(inflater, container, false)
        return binding.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        initView()
        collectFlow()
    }

    private fun initView() {
        adapter = LinkShortCutAdapter(this)
        binding.recyclerView.adapter = adapter
        binding.recyclerView.layoutManager = LinearLayoutManager(requireContext())

    }

    private fun collectFlow() {
        viewLifecycleOwner.lifecycleScope.launch {
            repeatOnLifecycle(Lifecycle.State.STARTED) {
                launch {
                    viewModel.linkShortCutFlow.collectLatest { result ->
                        when (result) {
                            is Result.Success -> {
                                adapter.submitList(result.data)
                            }

                            is Result.Error -> {
                                failToast(result.error.errorMessage)
                            }

                            else -> {

                            }
                        }
                    }
                }
            }
        }
    }

    override fun onLinkItemAdd(shortCut: ShortCutEntity) {
        viewLifecycleOwner.lifecycleScope.launch {
            when (val result = viewModel.addLinkShortCut(shortCut.id)) {
                is Result.Success -> {
                    successToast("添加成功")
                }

                is Result.Error -> {
                    failToast("添加失败")
                    LOG.d("error , ${result.error.errorMessage}")
                }

                else -> {

                }
            }
        }

    }
}